package com.csii.adhoc.dataready.serviceImpl;

import com.csii.adhoc.dataready.TDataConstant;
import com.csii.adhoc.dataready.TDataMergePo;
import com.csii.adhoc.dataready.mapper.TDataMergeMapper;
import com.csii.adhoc.dataready.service.TDataMergeService;
import com.csii.adhoc.exception.ParamValidityException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @description: 数据集接口实现类
 * @author: zyliu
 * @create: 2021-01-13 09:32
 */
@Service("tDataMergeService")
@Slf4j
public class TDataMergeServiceImpl implements TDataMergeService {

    @Autowired
    TDataMergeMapper tDataMergeMapper;

    @Override
    @Transactional
    public void tDataMergeMaintain(TDataMergePo tDataMergePo, String optMode) {
        //检查上传的值

        switch (optMode) {
            case TDataConstant.OPT_MODE_ADD:
                insertOneTDataMerge(tDataMergePo);
                break;
            case TDataConstant.OPT_MODE_UPDATE:
                updateTDataMergeByPrimaryKey(tDataMergePo);
                break;
            case TDataConstant.OPT_MODE_DELETE:
                deleteTDataMergeByPrimaryKey(tDataMergePo.getModelId());
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("操作标志不合法:" + optMode);
        }
    }

    @Transactional
    @Override
    public void insertOneTDataMerge(TDataMergePo tDataMergePo) {
        if (StringUtils.isEmpty(tDataMergePo.getModelId())) {
            tDataMergePo.setModelId(UUID.randomUUID().toString().replace("-", ""));
        }
        tDataMergeMapper.insertOneTDataMerge(tDataMergePo);
    }

    @Transactional
    @Override
    public int updateTDataMergeByPrimaryKey(TDataMergePo tDataMergePo) {
        return tDataMergeMapper.updateByPrimaryKey(tDataMergePo);
    }

    @Transactional
    @Override
    public int deleteTDataMergeByPrimaryKey(String modelId) {
        return tDataMergeMapper.deleteByModelId(modelId);
    }

    @Override
    public List<TDataMergePo> selectListTDataMerge(String modelId) {
        return tDataMergeMapper.selectListTDataMerge(modelId);
    }

    @Override
    public List<Map<String,String>> selectTableIdByModelId(String modelId) {
        return tDataMergeMapper.selectTableIdByModelId(modelId);
    }

    @Override
    public TDataMergePo selectOneTDataMerge(String modelId,String fldEnname) {
        return tDataMergeMapper.selectOneTDataMerge(modelId,fldEnname);
    }
    @Override
    public Map selCustDataDbTable(String sql){
        return tDataMergeMapper.selCustDataDbTable(sql);
    }
}
